import java.io.*;

public class SearchAndCopyRunnable implements Runnable{

    @Override
    public void run() {
        try {
            searchAndCopy("C:","txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }




    public static void searchAndCopy(String path,String type) throws IOException {
        File pathFile = new File(path);
        if (!pathFile.isDirectory()){
            return;
        }
        File[] files = pathFile.listFiles();
        if (files == null){
            return;
        }
        for (File file:files){
            if (file.isFile() && file.getName().endsWith(type)){
                System.out.println(file.getAbsoluteFile());
                copy(file.getAbsolutePath(),"E:/333");
            }
            if (file.isDirectory()){
                searchAndCopy(file.getAbsolutePath(),type);
            }
        }

    }

    public static void copy(String srcFilePath , String descDirPath) throws IOException {
        File srcFile = new File(srcFilePath);
        File descDirFile = new File(descDirPath+File.separator+srcFile.getName());
        if (!descDirFile.getParentFile().exists()){
            descDirFile.getParentFile().mkdirs();
        }
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(descDirFile));

        byte[] bufs = new byte[1024];
        int length = bufferedInputStream.read(bufs);
        while (length!=-1){
            bufferedOutputStream.write(bufs,0,length);
            length = bufferedInputStream.read(bufs);
        }
        bufferedInputStream.close();
        bufferedOutputStream.flush();
        bufferedOutputStream.close();
    }


}
